<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 *
 */
class Tourlist extends CI_Model  {
	/**
	 * Constructor
	 * @access protected
	 */
	function _construct()
	{
      // Call the Model constructor
      parent::_construct();


	}
	
	function show($page,$limit,$sidx,$sord,$type,$idUser){
		
		$query="SELECT COUNT(*) as count FROM listofservices";
		//$where=" ";
		if($idUser==0) #uesrType   0 - agent; 1 - prinimauwie; 2 - ADMIN
		{
			$where=" where idUser=".$this->session->userdata('idUser');
		}
		else
		{
			$where=" where idUser=".$idUser;
		}

		$query.=$where;
		//////$this->log($query);
		$result = $this->db->query($query);
		if ($result->num_rows() > 0)
		{
   			$row = $result->row();
			$count = $row->count;
		}
		else
		{
			$count=0;
		}

		if( $count >0 ) {
			$total_pages = ceil($count/$limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		if($start<0)$start=0;
		$query="SELECT idService FROM listofservices ".$where." order by id desc";
		$result=$this->db->query($query);

		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records = $count;
		$i=0;
		foreach($result->result() as $row)
		{
			$query="Select `id`,`name`,`desc` from service where idType=".$type." and id=".$row->idService;
			////$this->log($query);
			$line=$this->db->query($query);
			if ($line->num_rows() > 0)
			{
				$lineRow = $line->row();
				$edit="<img  class='edit_row_tourlist but' value='edit' mtype='$type' id='$lineRow->id' src='/css/images/edit.jpg'/> ";
				$go="<a  href='/admin/price/$type/".$lineRow->id."' ><img src='/css/images/price.jpg' class='but'/></a>";
				if($this->session->userdata('userType')!=0)
				{
					$responce->rows[$i]['cell']=array($lineRow->id,$lineRow->name,$lineRow->desc,$edit,$go);
				}
				else
				{
					$responce->rows[$i]['cell']=array($lineRow->id,$lineRow->name,$lineRow->desc,$go);	
				}
				$i++;
			}

		}
		$this->db->close();
		return json_encode($responce);
	}
	function edit($Id,$name, $desc)
	{
		
		$sql = "UPDATE service SET 	`name`='$name',
									`desc`='$desc'
									WHERE id=$Id";
		//$this->log($sql);
   		$this->db->query($sql);
   		$this->db->close();
	}
	function deleteTour($tourId)
	{
		//$this->log('deletetour id='.$tourId);
		$sql = "DELETE FROM service WHERE id=$tourId";
   		$this->db->query($sql);
   		$this->load->model('pricelist');
   		$this->pricelist->deleteTables($tourId);
   		$query="Delete FROM listofservices where idService=$tourId";
   		$this->db->query($query);
   		$query="delete FROM listoptions where idservice=$tourId";
   		$this->db->query($query);
	}
	function addtour($name,$desc,$type)
	{
		
		$sql="INSERT INTO service (
									`name`,
								   	`desc`,
								   	`idType`,
								   	`idUser`
								  ) VALUES(
								  	'$name',
								  	'$desc',
								  	$type,
								  	".$this->session->userdata('idUser')."
								  )";

		$this->db->query($sql);
		$query="Select id from service where idType=".$type." and idUser=".$this->session->userdata('idUser')." order by id desc";
		$result=$this->db->query($query);
		if ($result->num_rows() > 0)
		{
			$row = $result->row();
			$count = $row->id;
		}
		if($count!=0)
		{
			$query="INSERT INTO  `listofservices` (  `idUser` ,  `idService` ) VALUES ('".$this->session->userdata('idUser')."','$count')";
			$this->db->query($query);
		}
		$this->db->close();
	}
	function getServicesAdmin()
	{
		
		$query="Select idType from listoftypes where idUser=".$this->session->userdata('idUser');
		$result=$this->db->query($query);
		$types=array();
		foreach($result->result() as $row)
		{
			$query="Select id,name from type where id=".$row->idType;
			$resultTypes=$this->db->query($query);

			if($resultTypes->num_rows()>0)
			{
				$type=$resultTypes->row();
				$types[$type->id]=$type->name;
			}
		}



		$this->db->close();
		return $types;
	}
	function getServicesUser($type)
	{
		$query="Select idUser from podpisannie where idPodp=".$this->session->userdata('idUser');
		$result=$this->db->query($query);
		$i=0;
		$responce=array();
		foreach($result->result() as $row)
		{
			$query="select id,Country from users where id=".$row->idUser;
			$res=$this->db->query($query)->row();
			
			$responce[$i]['country']=$res->Country;
			$responce[$i]['User']=$res->id;
			//$query="Select idType from listoftypes where idUser=".$res->id." and idType=".$type;
			//$result2=$this->db->query($query);
			$types=array();
			//foreach($result2->result() as $row2)
			//{
				$query="Select id,name from type where id=".$type;
				$resultTypes=$this->db->query($query);
			
				if($resultTypes->num_rows()>0)
				{
					$type=$resultTypes->row();
					$types[$type->id]=$type->name;
				}
			//}
			$responce[$i]['types']=$types;
			$i++;
		}
		//print_r($responce);
		return $responce;
		
		
	}
	function addType($name)
	{
		
		$query="insert into type (`name`) values('$name')";
		//$this->log($query);
		if($this->db->query($query))
		{
			$responce['state']=true;
		}
		else
		{
			$responce['state']=false;
		}
		#$this->db->close();
		return json_encode($responce);
	}
	function getTypeList()
	{
		
		$query='select id,name from type';
		$result=$this->db->query($query);
		$responce=array();
		$html="<select id='addService' name='addService' width=220>";
		foreach($result->result() as $row)
		{
			$html.='<option value="'.$row->id.'">'.$row->name.'</option>';
		}
		$html.="</select>";
		$responce['state']=true;
		$responce['context']=$html;
		//$this->log("!!!!".$responce['context']);
		$this->db->close();
		return json_encode($responce);
	}
	function addService($id)
	{
		
		$query="insert into listoftypes (idUser,idType) values (".$this->session->userdata('idUser').",".$id.")";
		$this->db->query($query);
		$query="Select id,name from type where id=".$id;
		$result=$this->db->query($query);
		if($result->num_rows()>0)
		{
			$row=$result->row();
			$responce='<tr >
									<td width="2%"> &nbsp;</td>
									<td><h2>'.$row->name.'</h2></td>
									<td width="2%"> &nbsp;</td>
								</tr>
								<tr >
									<td width="2%"> &nbsp;</td>
									<td>
										<div id="pager'.$row->id.'"></div>
										<table id="table'.$row->id.'"></table>
										<script> createTable("'.$row->id.'");</script>
									</td>
									<td width="2%"> &nbsp;</td>
								</tr>
						';
		}
		else
		{
			$responce="";
		}
		$this->db->close();
		return $responce;
	}
	function cloneservice($id)
	{
		$query="select * from service where id=".$id;
		$result=$this->db->query($query);
		if($result->num_rows()>0)
		{
			$row=$result->row();
			$query='insert into service (name,`desc`,idtype,idUser) values ("'.$row->Name.'","'.$row->Desc.'","'.$row->idType.'","'.$row->idUser.'")';
			$this->db->query($query);
			$query="select id from service order by id desc limit 1";
			$result=$this->db->query($query);
			if($result->num_rows()>0)
			{
				$newId=$result->row()->id;
				$query='insert into listofservices (idservice,idUser) values ('.$newId.','.$row->idUser.')';
				$this->db->query($query);
				$query="select * from listoftables where idService=".$id;
				$result=$this->db->query($query);
				foreach($result->result() as $row)
				{
					$query="insert into listoftables (name,`desc`,idStruct,idService) values ('".$row->Name."','".$row->Desc."',".$row->idStruct.",".$newId.")";
					$this->db->query($query);
					$query="select id from listoftables order by id desc limit 1";
					$res=$this->db->query($query);
					if($res->num_rows()>0)
					{
						$newTabId=$res->row()->id;
						$query="select * from price where idtable=".$row->id;
						$prices=$this->db->query($query);
						foreach($prices->result() as $price)
						{
							$query="insert into price (idTable,idRow,`date`,price)  values ($newTabId,$price->idRow,'$price->Date',$price->Price)";
							$this->db->query($query);
						}
						
					}
					
				}
			}
			else
			{
				$this->log('error in query='.$query);
				die();
			}
		}
		else
		{
			$this->log('error in query='.$query);
			die();
		}
	}
}